package com.wuyan.web.base.config;

import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.PathBuilder;
import com.wuyan.web.base.entity.PubConfig;
import com.wuyan.web.base.entity.QPubConfig;
import com.wuyan.web.base.helper.PubConfigHelper;
import com.wuyan.web.base.helper.rep.RepPageData;
import com.wuyan.web.base.helper.req.CustomQueryHelper;
import com.wuyan.web.base.service.PubConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 启动配置
 *
 * @author wuyan
 */

@Slf4j
@Component
public class AfterRunnerConfig implements CommandLineRunner {

    @Autowired
    private PubConfigHelper configHelper;

    @Autowired
    private PubConfigService pubConfigService;

    @Override
    public void run(String... args) throws Exception {
        initConfig();
    }

    /**
     * 初始化配置
     */
    private void initConfig() {
        // 构建查询条件
        CustomQueryHelper<PubConfig> customQueryHelper = CustomQueryHelper.<PubConfig>builder()
                .paramsList(new ArrayList<>())
                .pathBuilder(new PathBuilder<>(PubConfig.class, QPubConfig.pubConfig.getMetadata()))
                .paramsOrders(new ArrayList<>())
                .entityClass(PubConfig.class)
                .build()
                .structure();
        List<Predicate> predicates = customQueryHelper.getPredicates();
        List<OrderSpecifier<?>> orders = customQueryHelper.getOrders();

        RepPageData<PubConfig> data = pubConfigService.page(predicates, orders, false, 1, 0);
        if (null != data.getList()) {
            configHelper.setConfigs(data.getList());
        }
    }
}
